﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace WindowsFormsApplication1
{
    public partial class LoginForm : Form
    {
        private int randomkey;
        
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(!checknoblank())
            {
                return;
            }
            
            if (!checkrandomkey())
            {
                generaterandomkey();
                return;
            }
            
            if (validateuser())
            {
                MonitorForm mf = new MonitorForm(this);
                mf.Show();
                this.Hide();
                UserLog.addloginrecord(User.UserID, User.Username, User.Userrole, "登陆", User.IPAddress);
            }
            else
            {
                generaterandomkey();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool checknoblank()
        {
            if(this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if(this.textBox2.Text == "")
            {
                MessageBox.Show("请输入密码");
                return false;
            }
            if(this.textBox3.Text == "")
            {
                MessageBox.Show("请输入验证码");
                return false;
            }
            if (this.textBox3.Text != this.label5.Text)
            {
                MessageBox.Show("请输入正确的验证码");
                return false;
            }
            return true;
        }

        private string getipaddress()
        {
            IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

            if (addressList[0].AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString())
            {
                return addressList[0].ToString();
            }
            else
            {
                IPAddress ip = IPAddress.IPv6Any;
                return ip.ToString();
            }
        }

        
        private bool validateuser()
        {
            int userid = Int32.Parse(this.textBox1.Text);
            string password = this.textBox2.Text;
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            var query = (from c in dbcontext.tb_user
                         where c.ID == userid && c.Password == password
                         select new { c.ID, c.Name, c.Role, c.Password });

            if (query.Count() == 0)
            {
                MessageBox.Show("没用该用户号码或密码错误");
                this.Close();
                return false;
            }

            User.UserID = query.First().ID;
            User.Username = query.First().Name;
            User.Userrole = query.First().Role;
            User.IPAddress = this.getipaddress();
            User.Password = query.First().Password;
            return true;
        }

        private void generaterandomkey()
        {
            Random ran = new Random();
            randomkey = ran.Next(1000, 9999);
            this.label5.Text = randomkey.ToString();
        }

        private bool checkrandomkey()
        {
            if (this.textBox3.Text == "")
            {
                MessageBox.Show("请输入验证码");
                return false;
            }
            return true;
            
           int key = Int32.Parse(this.textBox3.Text);
            if(key == randomkey)
            {
                return true;
            }
            else
            {
                MessageBox.Show("请输入正确的验证码");
                return false;
            }
        }
        

        private void label5_Click(object sender, EventArgs e)
        {
            generaterandomkey();  
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            generaterandomkey();
            this.textBox3.Text = null;
        }

      
    }
}